<!--<link href="--><?php //echo URL::base() ?><!--media/vendor/css/ui-lightness/jquery-ui-1.10.4.custom.css" rel="stylesheet">-->
<link href="<?php echo URL::base() ?>media/vendor/css/ui-lightness/jquery-ui-1.10.4.custom.css" rel="stylesheet">
<script src="<?php echo URL::base() ?>media/vendor/js/jquery-1.10.2.js"></script>

<script src="<?php echo URL::base() ?>media/vendor/js/jquery.ui.autocomplete.min.js"></script>

<script src="<?php echo URL::base() ?>media/laydate/laydate.js"></script>

<script src="<?php echo URL::base() ?>media/vendor/js/jquery-ui-1.10.4.custom.js"></script>
<!--<script src="--><?php //echo URL::base() ?><!--media/vendor/js/jquery.ui.datepicker-zh-TW.js"></script>-->
<script src="<?php echo URL::base() ?>media/vendor/artdialog/jquery.artDialog.js"></script>
<link rel="stylesheet" href="<?php echo URL::base() ?>media/vendor/artdialog/skins/default.css" type="text/css"
      media="all"/>
<script src="<?php echo URL::base() ?>media/vendor/artdialog/jquery.artDialog.js"></script>
<script src="<?php echo URL::base() ?>media/js/jquery.json-2.4.js"></script>
<script src="<?php echo URL::base() ?>media/layer/layer.js"></script>
<style type="text/css">
    .lab1 {
        /*width: 100px;*/
        text-align: right;
        display: inline-block;
       margin: 0px 6px 20px 20px;
        }

    #txtHint {
        position: absolute;
        top: 25px;
        left: 0;
        display: none;
        background-color: #EDFAF4;
        min-width: 281px;
        border: 1px solid #aaa;
        border-radius: 5px;
        height: 150px;
        overflow: auto;
        }

    #txtHint li {
        list-style: none;
        color: #333;
        border: 1px solid #EDFAF4;
        padding: 0px 8px;
        }

    #txtHint li:hover {
        color: #c37100;
        background-color: #fdf8d5;
        border: 1px solid #fac900;
        border-radius: 25px;
        }

    #detail span:hover {
        color: #c37100;
        }
    .controls select{width: 284px;}

    #group {
        position: relative;
        }
</style>
<div class="row-fluid">
    <form action="<?php echo URL::site() ?>purchases/select/purchaseindex" method="GET" class="form-horizontal">
        <div class="control-group">
            <label class="control-label">预约单时间</label>
            <div class="controls">
            <input class="input-medium" type="text"
                   value="<?php echo isset($_GET['date_from'])?$_GET['date_from']:''?>" name='date_from' id="date_from">
            到
            <input class="input-medium" type="text"
                   value="<?php echo isset($_GET['date_to'])?$_GET['date_to']:''?>" name='date_to' id="date_to"/>
            </div>
        </div>
        <div class="control-group">
             <label class="control-label">预约单号</label>
            <div class="controls">
            <input class="input-xlarge" type="text"
                                                  value="<?php echo isset($_GET['purchaseorder_id'])?$_GET['purchaseorder_id']:'';?>"
                                                  name='purchaseorder_id'/>
             </div>
        </div>

        <div class="control-group">
            <label class="control-label">供应商</label>
        <div class="controls">
            <input type="hidden" class="input-mini" name="supplier_id" id="supplier_id" value="<?php echo isset($_GET['supplier_id'])?$_GET['supplier_id']:'';?>" autocomplete="off" readonly>
            <input name="supplier_name" id="supplier_name" value="<?php echo isset($_GET['supplier_name'])?$_GET['supplier_name']:'';?>"
                    class="input-xlarge" type="text"/>
            </div>
        </div>
        <div class="control-group">
            <label class="control-label">SKU</label>
            <div class="controls">
                    <input class="input-xlarge" type="text" value="<?php echo isset($_GET['sku'])?$_GET['sku']:'';?>" name='sku'/>
            </div>
        </div>
        <div class="control-group">
             <label class="control-label">付款方式</label>
            <div class="controls">
            <select class="input-xlarge" name='payway' id="payway">
                <option value="">请选择</option>
                <?php if (isset($payway) && !empty($payway)) {
                    foreach ($payway as $storeId => $store) {
                        ?>
                        <option value="<?php echo $storeId; ?>" <?php if (isset($_GET['payway']) && $_GET['payway'] == $storeId) {
                            echo "selected='selected'";
                        } ?>><?php echo $store; ?></option>
                    <?php }
                } ?>
            </select>
            </div>
        </div>
        <div class="control-group">
            <label class="control-label">仓库</label>
            <div class="controls">
            <select class="input-xlarge" name='store' id="store">
                <option value="">请选择</option>
                <?php if (isset($stores) && !empty($stores)) {
                    foreach ($stores as $storeId => $store) {
                        ?>
                        <option value="<?php echo $storeId; ?>" <?php if ($_GET['store'] == $storeId) {
                            echo "selected='selected'";
                        } ?>><?php echo $store; ?></option>
                    <?php }
                } ?>
            </select>
            </div>
        </div>
        <div class="control-group">
            <label class="control-label">主体</label>
            <div class="controls">
                <select class="input-xlarge" name='enterprise_dominant' id="enterprise_dominant">
                    <option value="">全部</option>
                    <?php foreach ($enterprise_dominant as $key =>$val) {?>
                        <option value="<?php echo $key?>"><?php echo $val?></option>
                    <?php }?>
                </select>
            </div>
        </div>
        <div class="control-group">
            <label class="control-label">状态</label>
            <div class="controls">
            <select class="input-xlarge" name='status' id="status">
                <option value="">全部</option>
                <?php if (isset($status) && !empty($status)) {
                    foreach ($status as $id => $name) {
                        ?>
                        <option value="<?php echo $id; ?>" <?php if (isset($_GET['status']) && $_GET['status'] == $id) {
                            echo "selected='selected'";
                        } ?>><?php echo $name; ?></option>
                    <?php }
                } ?>
            </select>
            </div>
        </div>

            <div class="control-group">
             <label class="control-label">计划员</label>
                <div class="controls">
                    <input type="text" class="input-mini" name="purchaseplaner_id" id="purchaseplaner_id" value="<?php echo $_GET['purchaseplaner_id']?>" autocomplete="off" readonly>
                    <input  name="purchaseplaner" id="purchaseplaner" type="text" value="<?php echo $_GET['purchaseplaner']?>" autocomplete="off">
                </div>
            </div>
        <div class="control-group">
            <div class="controls">
                <button class="btn btn-info" type="submit">查询</button>
                <button class="btn btn-default" onclick="resetdata()" type="button">重置</button>
                <input class="btn btn-default" name="down" type="submit" value="导出">
            </div>
        </div>
    </form>
</div>
<div class="row-fluid">
    <div class="span12">
        <?php if (isset($result) && $result) { ?>
            <table class="table table-hover ">
                <thead>
                <tr style=" background-color:#4EA392; color:#fff;">
                    <th>
                        <input type="checkbox" id="chooseall"/>
                    </th>
                    <th width="100">VMI预约单号</th>
                     <th>供应商</th>
                    <th>预约单时间</th>
                    <th>计划员</th>
                    <th>总金额</th>
                    <th>付款方式</th>
                    <th>状态</th>
                    <th>主体</th>
                    <th>仓库</th>
                    <th width="180" >备注</th>
                    <th width="50">操作</th>
                </tr>
                </thead>
                <tbody>
                <?php foreach ($result as $re) {
                    // 						var_dump($re);exit;
                    ?>
                    <tr id="select_<?php echo $re['id']; ?>">
                        <td>
                            <input type="checkbox" name="choose" value="<?php echo $re['id']; ?>"/>
                        </td>
                        <td>
                            <a href="<?php echo URL::site() ?>purchases/selectpurchase/singledetail?p_id=<?php echo $re['id'] ?>" style="color: red"><?php echo $re['id']; ?></a>
                        </td>
                        <td><?php echo $re['supplier_id'] ?></td>
                        <td><?php echo $re['purchaseorder_date'] ?></td>
                        <td><?php echo $re['purchaseplaner'] ?></td>
                        <td><?php echo number_format($re['money'], 2); ?></td>
                        <td><?php echo $re['pay_way_name']; ?></td>
                        <td class="status"><?php echo $status[$re['status']];echo ($re['status']>=20&&$re['status']<70&&$re['is_pull']==0)?'(未推送)':''; ?></td>
                        <td><?php echo $re['enterprise_dominant']; ?></td>
                        <td><?php echo $re['storeName']; ?></td>
                        <td><div style="max-width: 175px; height: 36px; overflow-y: auto;">
                            <?php echo $re['remark'] ?>
                        </div>
                        </td>
                        <td>
                            <div class="btn-group">
                                <a role="menuitem" href="<?php echo URL::site() ?>purchases/selectpurchase/singledetail?p_id=<?php echo $re['id'] ?>" target="_blank" class="btn btn-info btn-small" >查看</a>
                                <?php if($re['status']<70) { ?>
                                <button class="btn btn-info btn-small dropdown-toggle" data-toggle="dropdown">
                                    <span class="caret"></span>
                                </button>
                                <?php }else{ ?>
                                <button class="btn btn-info btn-small dropdown-toggle disabled" disabled data-toggle="dropdown">
                                    <span class="caret"></span>
                                </button>
                                <?php } ?>
                                <ul class="dropdown-menu">
                                    <?php if($re['status'] < 20) { ?>
                                    <li><a role="menuitem" href="<?php echo URL::site() ?>printpurchase/purchaseprint?ids=<?php echo $re['id'] ?>&condition=<?php echo $condition;?>" target="_blank">打印</a></li>
                                    <?php } ?>
                                    <?php if($re['status'] < 70) { ?>
                                    <li><a role="menuitem" href="javascript:;" onclick="show('<?php echo $re['id'] ?>',10)">取消</a></li>
                                    <?php } ?>
                                    <?php if($re['status']>=20&&$re['status']<70&&$re['is_pull']==0) { ?>
                                    <li><a role="menuitem" href="javascript:;" onclick="pull('<?php echo $re['id'] ?>')">重新推送</a></li>
                                    <?php } ?>
                                </ul>
                            </div>
                        </td>
                    </tr>
                <?php } ?>
                </tbody>
            </table>
            <div align="center">
                <form action="<?php echo URL::base(TRUE); ?>printpurchase/purchaseprint" method="post" id="printform"
                      target="_blank">
                    <input type="text" name="ids" id="ids" value="" style="display:none"/>
                    <input type="text" name="condition" id="condition"
                           value="<?php if (isset($condition))
                               echo $condition ?>" style="display:none"/>
                </form>
                <input type="button" name="print" value="打印" class="btn btn-success" id="print"/>
            </div>
        <?php } ?>
        <?php if (isset($pagination)) {
            echo $pagination;
        } ?></div>
</div>
<script type="text/javascript">
    $(document).ready(function () {
        $('.table-hover > tbody > tr:odd').css("background-color", "#f5f5f5");
    })

    $(function () {
        $("#supplier_id").keyup(function (e) {
            var $val = $(this).val();
            showHint($val);
        });
        $('#enterprise_dominant').val('<?php echo $_GET['enterprise_dominant']?>');
        $('#status').val('<?php echo $_GET['status']?>');
        $('#payway').val('<?php echo $_GET['payway']?>');
    });
    function showHint(str) {
        if (str.length == 0) {
            $("#supplier_id").val('');
            return false;
        }
        var url = "<?php echo URL::base() ?>purchase/get_supplier";
        url += "?name=" + str;
        url += "&sid=" + Math.random();
        $.getJSON(url, function (data) {
            $("#txtHint").html("");
            var $array = data.split(',');
            $.each($array, function (index, value) {
                $('<li>' + value + '</li>').appendTo($("#txtHint"));
                $("#txtHint").show();
                $('#txtHint li').click(function () {
                    $("#supplier_id").val($(this).text());
                    $(this).parent().hide();
                });
            });
        });
    }
</script>
<script type="text/javascript">
    $(document).ready(function () {

        laydate.render({
            elem: '#date_from'
        });
        laydate.render({
            elem: '#date_to'
        });
        //初始化checkbox的状态
        $("input[type='checkbox']").each(function () {
            if (this.checked) {
                this.checked = false;
            }
        });

        var ids = "-";
        //点击全选和全不选
        $("#chooseall").click(function () {
            if (this.checked) {
                $("input[name='choose']").each(function () {
                    this.checked = true;

                    //如果该复选框已经被选择，则获取其采购单的ID
                    if (this.checked) {
                        var id = $(this).parent().next().text();
                        if ($("#show_" + $.trim(id)).val() == 0) {
                            $("#print").attr("disabled", true);
                            return;
                        }
                        if (ids.indexOf(id) == -1) {
                            ids = ids + id + "-";
                        }
                    }
                });
            } else {
                $("#print").attr("disabled", false);
                ids = "-";
                $("input[name='choose']").each(function () {
                    this.checked = false;
                });
            }
        });
        //点击单个单选按钮将其id拼入或者删除
        $("input[name='choose']").click(function () {
            var id = $(this).parent().next().text();
            if (this.checked) {
                if ($("#show_" + $.trim(id)).val() == 0) {
                    $("#print").attr("disabled", true);
                    return;
                }
                if (ids.indexOf(id) == -1) {
                    ids = ids + id + "-";
                }

            } else {
                $("#print").attr("disabled", false);
                var index = ids.indexOf(id);
                if (index != -1) {
                    var idremove = ids.substr(index, id.length);
                    var reg = "-" + idremove + "-";
                    ids = ids.replace(new RegExp(reg), "-");
                }
            }
        });
        //点击打印按钮，调用后台php代码
        $('#print').click(function () {
            var list = [],msg='';
            $('input[name=choose]:checked').each(function () {
                if($.trim($(this).parent('td').siblings('.status').text())=='取消'){
                    msg +='预约单号：'+$(this).val()+'为取消状态，不能打印预约单\n';
                }
                  list.push($(this).val());
            });
            if(msg!=''){
                layer.alert(msg, {icon: 5});
                return false;
            }
            if (list.length == 0) {
                layer.alert('请选择需要打印的采购单', {icon: 5});
                return;
            } else {
                //alert(ids);return ;
                $('#ids').val(list);
                $("#printform").submit();
            }
        });

    });
//    $("#supplier_name").autocomplete({
//        autoFocus: true,
//        minLength: 0,
//        autoFill: true,
//        source: '<?php //echo URL::base(true)?>//purchase/get_supplier',
//        select: function (event, ui) {
//            /**
//             * 从建议列表中选中之后触发的事件
//             */
//            $("#supplier_name").val(ui.item.name);
//            $("#supplier_id").val(ui.item.id);
////            $("#supplier_name").attr("readonly", "readonly");
////            get_supplier_info(ui.item.id);
//            return false;
//        }
//    });
    /**
     * 计划员自动提示补全
     */
    $("#purchaseplaner").autocomplete({
        autoFocus: true,
        minLength: 0,
        autoFill: true,
        source: '<?php echo URL::base(true)?>purchase/get_users',
        select: function (event, ui) {
            /**
             * 从建议列表中选中之后触发的事件
             */
            $("#purchaseplaner").val(ui.item.name);
            $("#purchaseplaner_id").val(ui.item.id);
            return false;
        }
    });
    //修改采购单
    function change_purchase(purchaseorder_id) {
        //var money=$("#money"+purchaseorder_id).val();
        //	var trans_fee=$("#trans_fee"+purchaseorder_id).val();//alert(trans_fee);return ;
//		var remark = $("#remark"+purchaseorder_id).val();
        $.get("<?php echo URL::base() ?>purchase/changepurchase",
            {purchaseorder_id: purchaseorder_id},
            function (data) {
                if (data == 0) {
                    layer.alert('采购单状态不适合修改', {icon: 5});
                    return;
                }
                var obj = jQuery.parseJSON(data);
                art.dialog({
                    title: '采购单详情',
                    lock: true,
                    opacity: 0.87,	// 透明度
                    content: obj.view,
                    button: [{
                        name: '关闭',
                        callback: function () {
                            window.location.reload();

                        }
                    }]
                });
            });
    }
    //重置条件
    function resetdata() {
        $(".input-medium").val("");
        $(".input-xlarge").val("");
        $(".input-mini").val("");
        $("#purchaseplaner").val("");
//        $("#status").val("1");
    }
    //实现弹层
    /*
     function show(id) {
     var Idiv = document.getElementById('modify_value');
     //document.getElementById('add_model').value=document.getElementById('txt_model').value;
     Idiv.style.display = "block";
     // $("#add_value #add_model").focus();
     Idiv.style.left = (document.documentElement.clientWidth - Idiv.clientWidth)/2+ "px";
     Idiv.style.top = (document.documentElement.clientHeight - Idiv.clientHeight)/2 + "px";
     document.body.style.overflow = "scroll";

     }
     window.onload = function ()
     {
     var oLay = document.getElementById("modify_value");
     var oClose = document.getElementById("close");

     oClose.onclick = function ()
     {
     oLay.style.display = "none";
     oClose.style.display="none";
     };

     };*/
    //实现弹层
    function show(p_id,type) {
        layer.confirm('你确定要删除吗？', {
            btn: ['确定','取消'] //按钮
        }, function(){
            $.get("<?php echo URL::base() ?>purchases/select/delPurchaseOrder",
                {p_id: p_id,type:type},
                function (data) {
                    layer.alert(data, {icon: 6},function () {
                        window.location.reload();
                    });
                });
        }, function(){
        });
    }
    function pull(p_id) {
        layer.confirm('你确定要重新推送吗？', {
            btn: ['确定','取消'] //按钮
        }, function(){
            $.get("<?php echo URL::base() ?>purchases/select/pull",
                {id: p_id},
                function (data) {
                    layer.alert(data, function () {
                        window.location.reload();
                    });
                });
        }, function(){
        });
    }

</script>

